package com.mao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mao.common.Result;
import com.mao.dto.DishDto;
import com.mao.dto.SetmealDto;
import com.mao.entity.Category;
import com.mao.entity.Setmeal;
import com.mao.entity.SetmealDish;
import com.mao.service.CategoryService;
import com.mao.service.SetmealDishService;
import com.mao.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success("套餐保存成功");
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.page(page, pageSize, name);
        return Result.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> remove(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("删除套餐成功");
    }

    /**
     * 获取单个套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getWithDish(id);
        return Result.success(setmealDto);
    }

    /**
     * 更新套餐数据
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return Result.success("套餐更新成功");
    }

    /**
     * 起售或者停售套餐
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result<String> status(@PathVariable("status") Integer status,@RequestParam("ids") List<Long> ids){
        setmealService.status(status,ids);
        return Result.success("套餐状态修改成功");
    }

    /**
     * 查询所有套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public Result<List<SetmealDto>> list(Setmeal setmeal){
        List<SetmealDto> setmealDtoList = setmealService.get_list(setmeal);
        return Result.success(setmealDtoList);
    }

    /**
     * 查询关联的菜品
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public Result<List<DishDto>> getWithDishById(@PathVariable Long id){
        List<DishDto> dishDtoList = setmealService.getWithDishDto(id);
        return Result.success(dishDtoList);
    }
}
